/**
 * 
 */
var bibao = 30;
var round = 0;
$(document)
        .ready(
               function(){
	               for (var i = 0; i < 15; i++)
		               $("table")
		                       .append(
		                               "<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
	               var allPlace = new Array();
	               for (var i = 0; i < 15; i++)
		               allPlace[i] = new Array();
	               // 执黑
	               $("#bFirst").click(function(){
		               $("#wFirst").unbind("click");
		               $("#bFirst").unbind("click");
		               // alert("您选择了先手执黑");
		               $("td").click(function(){
			               $(this).unbind("click");
			               var b = $(this).index();
			               var a = $(this).parent().index();
			               var local = b * 15 + a;
			               $(this).css("background-image", "url('css/tdblack.jpg')");
			               if (evaluate(allPlace, 1, a, b) > 5000000){
				               alert("游戏结束,您赢了!");
				               $("td").unbind("click");
				               return;
			               }
			               allPlace[a][b] = 1;
			               var loc2;
			               if (++round < 3)
				               loc2 = noThink(allPlace, -1);
			               else
				               loc2 = aiThink(allPlace, -1, bibao);
			               b = loc2 % 15;
			               a = (loc2 - b) / 15;
			               $("tr").eq(a).find("td").eq(b).unbind("click");
			               $("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdwhite.jpg')");
			               allPlace[a][b] = -1;
		               });
	               });

	               // 执白
	               $("#wFirst").click(function(){
		               $("#wFirst").unbind("click");
		               $("#bFirst").unbind("click");
		               // alert("您选择了后手执白");
		               allPlace[7][7] = -1;
		               $("td").eq(112).css("background-image", "url('css/tdblack.jpg')")
		               $("td").click(function(){
			               $(this).unbind("click");
			               var b = $(this).index();
			               var a = $(this).parent().index();
			               var local = b * 15 + a;
			               $(this).css("background-image", "url('css/tdwhite.jpg')");
			               if (evaluate(allPlace, 1, a, b) > 5000000){
				               alert("游戏结束,您赢了!");
				               $("td").unbind("click");
				               return;
			               }
			               allPlace[a][b] = 1;
			               var loc2;
			               if (++round < 3)
				               loc2 = noThink(allPlace, -1);
			               else
				               loc2 = aiThink(allPlace, -1, bibao);
			               b = loc2 % 15;
			               a = (loc2 - b) / 15;
			               $("tr").eq(a).find("td").eq(b).unbind("click");
			               $("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdblack.jpg')");
			               allPlace[a][b] = -1;
		               });
	               });

               });

// 全棋盘分析
function aiThink(arr,sign,deep){
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (arr[i][j] != undefined || !check(arr, i, j))
				continue;
			arr[i][j] = sign;
			var vi=charge(arr,0-sign);
			arr[i][j]=undefined;
			if (vi)
				continue;
			else
				return i * 15 + j;
		}
	return noThink(arr, sign);
}

function sThink(arr,sign){
	var score = 0;
	var local = -1;
	var all = new Array();
	// 循环分析
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (arr[i][j] != undefined || !check(arr, i, j))
				continue;
			var temp = evaluate(arr, sign, i, j);
			all.push([i * 15 + j, temp]);
		}
	return all;
}
// 判断
function charge(arr,sign){
	// 分数组
	var my = sThink(arr, sign);
	var mysc = search(my, 5000);
	if (mysc.length > 0){
		
		return true;
	}
	// 对手分数组
	var op = sThink(arr, 0 - sign);
	var opsc = search(op, 5000);
	if (opsc.length > 1){
		alert(arr);
		console.log("nishule");
		return false;
	}
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;

	mysc = search(my, 4000);
	if (mysc.length > 0)
		return true;
	mysc = search(my, 3004);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	mysc = search(my, 400);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	opsc = search(op, 4000);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;
	opsc = search(op, 3004);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;

	mysc = search(my, 3030);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	mysc = search(my, 3020);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	mysc = search(my, 3000);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;

	opsc = search(op, 3030);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;
	opsc = search(op, 3020);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;
	opsc = search(op, 3000);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;

	mysc = search(my, 2003);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	mysc = search(my, 2020);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;
	return false;

	opsc = search(op, 2003);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;
	mysc = search(my, 2000);
	if (mysc.length > 0)
		if (nextV(arr, sign, mysc))
			return true;

	opsc = search(op, 2020);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;
	opsc = search(op, 2000);
	if (opsc.length > 0)
		if (nextV(arr, sign, opsc))
			return true;

	return false;
}

// 下一步
function nextV(arr,sign,opsc){
	for (var i = 0; i < opsc.length; i++){
		var victory;
		var b = opsc[i] % 15;
		var a = (opsc[i] - b) / 15;
		arr[a][b] = sign;
		victory = charge(arr, 0 - sign);
		arr[a][b] = undefined;
		if (victory)
			continue;
		else
			return true;
	}
	return false;
}

// 搜索
function search(all,score){
	var result = [];
	for (var j = 0; j < all.length; j++){
		if (all[j][1] == score)
			result.push(all[j][0]);
	}
	return result;
}
function bsearch(all,sign,score){
	var result = [];
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (evaluate(all, sign, i, j) == score)
				result.push(i * 15 + j);
		}
	return result;
}
// 简单
function noThink(arr,sign){
	var score = 0,local=0;
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if ( arr[i][j] != undefined||!check(arr, i, j) )
				continue;
			var temp = evaluate(arr, sign, i, j)+evaluate(arr,0-sign,i,j);
			if (score < temp){
				score = temp;
				local = i * 15 + j;
			}
		}
	return local;
}
// 位置打分
function evaluate(arr,sign,a,b){
	var anti = 0 - sign;
	var c = a + 5 < 15 ? a + 5 : 15,d = b + 5 < 15 ? b + 5 : 15,e = a - 4 > 0 ? a - 4 : 0,f = b - 4 > 0 ? b - 4 : 0;
	var score = 0;
	var troop = new Array();
	for (var i = 0; i < 4; i++)
		troop[i] = "";
	// 横
	for (var i = e; i < d; i++){
		if (i == b)
			troop[0] += '1';
		else if (arr[a][i] == sign)
			troop[0] += '1';
		else if (arr[a][i] == anti)
			troop[0] += '2';
		else
			troop[0] += '0';

	}

	// 竖
	for (var i = e; i < c; i++){
		if (i == a)
			troop[1] += '1';
		else if (arr[i][b] == sign)
			troop[1] += '1';
		else if (arr[i][b] == anti)
			troop[1] += '2';
		else
			troop[1] += '0';

	}

	// 捺\
	var i,j;
	for (i = e, j = f; i < c && j < d; i++, j++){
		if (i == a && j == b)
			troop[2] += '1';
		else if (arr[i][j] == sign)
			troop[2] += '1';
		else if (arr[i][j] == anti)
			troop[2] += '2';
		else
			troop[2] += '0';
	}

	// 撇/
	for (i = e, j = d - 1; i < c && j >= f; i++, j--){
		if (i == a && j == b)
			troop[3] += '1';
		else if (arr[i][j] == sign)
			troop[3] += '1';
		else if (arr[i][j] == anti)
			troop[3] += '2';
		else
			troop[3] += '0';
	}

	// 计算评分
	for (var i = 0; i < 4; i++){
		if (/(11111)/g.test(troop[i]))
			score += 20000000;
		else if (/(011110)|(1011101)/g.test(troop[i]))
			score += 500000;
		else if (/(11110)|(10111)|(01111)|(11101)|(11011)/g.test(troop[i]))
			score += 250000;
		else if (/(011100)|(001110)|(010110)|(011010)/g.test(troop[i]))
			score += 50000;
		else if (/(001100)|(011000)|(000110)|(010100)|(010010)|(001010)/g.test(troop[i]))
			score += 5000;
		else if (/(11100)|(10110)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)(001010)|(010010)/g
		        .test(troop[i]))
			score += 500;
		else if (/(11000)|(10010)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)(001010)|(010010)/g
		        .test(troop[i]))
			score += 50;
	}

	if (score > 20000000)
		return 5000;
	else if (score > 500000)
		return 4000;
	else if (score > 300000)
		return 3004;
	else if (score > 255000)
		return 2004;
	else if (score > 250000)
		return 400;
	else if (score > 100000)
		return 3030;
	else if (score > 55000)
		return 3020;
	else if (score > 50000)
		return 3000;
	else if (score > 10000)
		return 2020;
	else if (score > 5500)
		return 2003;
	else if (score > 5000)
		return 2000;
	else if (score > 550)
		return 30;
	else if (score > 50)
		return 20;
	else
		return 1 - Math.abs(a - 7) - Math.abs(b - 7);;
}

// 排除
function check(arr,a,b){
	var aa = a + 4 < 15 ? a + 4 : 15;
	var bb = b + 4 < 15 ? b + 4 : 15;
	for (var i = a - 4 > 0 ? a - 4 : 0; i < aa; i++)
		for (var j = b - 4 > 0 ? b - 4 : 0; j < bb; j++)
			if (arr != undefined)
				return true;
	return false;
}
